/*
 * @Author: Lee 497232807@qq.com
 * @Date: 2023-07-19 09:25:33
 * @LastEditors: Lee 497232807@qq.com
 * @LastEditTime: 2023-07-26 16:09:14
 * @FilePath: \ccc-hot-update\assets\script\common\Utils.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { AssetManager, screen, sys } from "cc";
import { assetManager } from "cc";
import { NATIVE } from "cc/env";
import {  SubGameDirName, WriteableDir} from "./VersionManager";
import { macro } from "cc";
import { view } from "cc";
import { Size } from "cc";
import { ResolutionPolicy } from "cc";
import { native } from "cc";


/**
 * 方向枚举
 */
export enum Dirction {
    PORTRAIT = macro.ORIENTATION_PORTRAIT,
    LANDSCAPE = macro.ORIENTATION_LANDSCAPE,
    AUTO = macro.ORIENTATION_AUTO,
}

export class Utils {

    /**
     * 加载bundle
     * @param bundleName 
     * @param callback 
     */
    static loadBundle(bundleName: string, callback:(err: Error | null, data: AssetManager.Bundle)=>void) {
        
        assetManager.loadBundle(bundleName, (err, bundle) => {
            callback(err, bundle);
        })
    }

      /**
     * 获取当前屏幕方向
     * @returns 
     */
    static getOrientation(): Dirction {
        return this.curDir;
    }    
    /**
     * 当前屏幕方向
     */
    protected static curDir:Dirction = Dirction.PORTRAIT
    /**
     * 设计尺寸
     */
    protected static designSize:Size =null
    /**
     * 改变屏幕方向
     * @param orientation   
     */
    //https://github.com/cocos/cocos-awesome-tech-solutions/tree/3.6.x-release/demo/Creator3.6.2_2D_ScreenSwitch
    static changeScreenOrientation(orientation:Dirction)
    {
        
       if(this.curDir == orientation) //当前方向和要改变的方向一致
        {
            return
        }    
        this.curDir = orientation
        let frameSize = screen.windowSize;
        if(this.designSize==null)//保存下当前设计分辨率
        {
            this.designSize = view.getDesignResolutionSize();
        }

        if(NATIVE)//原生平台 暂时屏蔽
        {
            var dir = this.curDir == Dirction.PORTRAIT?'V':'H';
            if (sys.os == sys.OS.ANDROID)
                native.reflection.callStaticMethod('com/cocos/game/AppActivity', 'setOrientation', '(Ljava/lang/String;)V', dir)
            else if (sys.os == sys.OS.IOS)
                native.reflection.callStaticMethod('AppDelegate', 'setOrientation:', dir)
        }
        
        if (this.curDir == Dirction.PORTRAIT) { //竖屏
            view.setOrientation(macro.ORIENTATION_PORTRAIT)
            if (frameSize.width > frameSize.height)
                screen.windowSize = new Size(frameSize.height, frameSize.width)
            view.setDesignResolutionSize(this.designSize.width, this.designSize.height, ResolutionPolicy.FIXED_HEIGHT)
        }
        else if(this.curDir == Dirction.LANDSCAPE) //横屏
        { 
            view.setOrientation(macro.ORIENTATION_LANDSCAPE)
            if (frameSize.height > frameSize.width)
                screen.windowSize = new Size(frameSize.height, frameSize.width)
            view.setDesignResolutionSize(this.designSize.height, this.designSize.width, ResolutionPolicy.FIXED_WIDTH);

        }
        // 手机浏览器切换
        if (sys.platform == sys.Platform.MOBILE_BROWSER) {
            window.dispatchEvent(new Event('resize'));
        }
    }

}